package com.example.demo.design.demo02_strategy;

/**
 * Created by 蔡超 on 2018/10/1.
 */
public class CashContext {
    private CashStrategy cashStrategy;

    public CashContext(CashStrategy cashStrategy) {
        this.cashStrategy = cashStrategy;
    }

    public CashContext(String type) {
        CashStrategy cashStrategy = null;
        switch (type){
            case "normal" : cashStrategy = new CashNormal();break;
            case "rebate" : cashStrategy = new CashRebate();break;
            case "return" : cashStrategy = new CashReturn();break;
        }
        this.cashStrategy = cashStrategy;
    }

    public double getReturn(double money){
        return cashStrategy.cashMethod(money);
    }

    public static void main(String[] args) {
        //工厂模式
        CashContext cashContext = new CashContext(new CashRebate());
        double d = cashContext.getReturn(300);
        System.out.println("d: " + d);

        //策略模式+工厂模式
        CashContext cashContext2 = new CashContext("return");
        double d2 = cashContext2.getReturn(300);
        System.out.println("d2: " + d2);

    }

}
